package com.cskaoyan.market.service;

import com.cskaoyan.market.db.domain.MarketCoupon;
import com.cskaoyan.market.db.domain.MarketCouponExample;
import com.cskaoyan.market.db.domain.MarketCouponUser;
import com.cskaoyan.market.db.domain.MarketCouponUserExample;
import com.cskaoyan.market.db.mapper.MarketCouponMapper;
import com.cskaoyan.market.db.mapper.MarketCouponUserMapper;
import com.cskaoyan.market.util.MybatisUtils;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class AdminCouponServiceImpl implements AdminCouponService {
    @Override
    public List<MarketCoupon> list(String page, String limit, String sort, String order,String name,String type,String status) {
        SqlSession session= MybatisUtils.getSession();
        MarketCouponMapper marketCouponMapper=session.getMapper(MarketCouponMapper.class);
        MarketCouponExample marketCouponExample =new MarketCouponExample();
        MarketCouponExample.Criteria criteria=marketCouponExample.createCriteria();
        if(!StringUtils.isEmpty(name)){
            criteria.andNameLike("%"+name+"%");
        }
        if (!StringUtils.isEmpty(type)){
            criteria.andTypeEqualTo(Short.valueOf(type));
        }
        if (!StringUtils.isEmpty(status)){
            criteria.andStatusEqualTo(Short.valueOf(status));
        }
        marketCouponExample.setOrderByClause(sort + " " + order);
        PageHelper.startPage(Integer.parseInt(page), Integer.parseInt(limit));
        List<MarketCoupon> marketCoupon = marketCouponMapper.selectByExample(marketCouponExample);
        session.close();
        return marketCoupon;
    }

    @Override
    public MarketCoupon read(String id) {
        SqlSession session= MybatisUtils.getSession();
        MarketCouponMapper marketCouponMapper=session.getMapper(MarketCouponMapper.class);
        MarketCouponExample marketCouponExample =new MarketCouponExample();
        MarketCouponExample.Criteria criteria=marketCouponExample.createCriteria();
        if(!StringUtils.isEmpty(id)){
            criteria.andIdEqualTo(Integer.valueOf(id));
        }
        MarketCoupon marketCoupon = marketCouponMapper.selectByPrimaryKey(Integer.valueOf(id));
        session.close();
        return marketCoupon;
    }

    @Override
    public List<MarketCouponUser> couponlist(String page, String limit, String sort, String order, String couponId, String userId, String status) {
        SqlSession session= MybatisUtils.getSession();
        MarketCouponUserMapper marketCouponUserMapper=session.getMapper(MarketCouponUserMapper.class);
        MarketCouponUserExample marketCouponUserExample =new MarketCouponUserExample();
        MarketCouponUserExample.Criteria criteria=marketCouponUserExample.createCriteria();
        if(!StringUtils.isEmpty(userId)){
            criteria.andUserIdEqualTo(Integer.valueOf(userId));
        }
        if(!StringUtils.isEmpty(status)){
            criteria.andStatusEqualTo(Short.valueOf(status));
        }
        if(!StringUtils.isEmpty(couponId)){
            criteria.andCouponIdEqualTo(Integer.valueOf(couponId));
        }
        marketCouponUserExample.setOrderByClause(sort + " " + order);
        PageHelper.startPage(Integer.parseInt(page), Integer.parseInt(limit));
        List<MarketCouponUser> marketCouponUser = marketCouponUserMapper.selectByExample(marketCouponUserExample);
        session.close();
        return marketCouponUser;
    }

    @Override
    public Integer addCoupon(MarketCoupon marketCoupon) {
        SqlSession session= MybatisUtils.getSession();
        MarketCouponMapper marketCouponMapper=session.getMapper(MarketCouponMapper.class);
        MarketCouponExample marketCouponExample =new MarketCouponExample();
        MarketCouponExample.Criteria criteria=marketCouponExample.createCriteria();
        Integer count =marketCouponMapper.insertSelective(marketCoupon);
        session.commit();
        session.close();
        return count;
    }

    @Override
    public Integer updateCoupon(MarketCoupon marketCoupon) {
        SqlSession session= MybatisUtils.getSession();
        MarketCouponMapper marketCouponMapper=session.getMapper(MarketCouponMapper.class);
        MarketCouponExample marketCouponExample =new MarketCouponExample();
        MarketCouponExample.Criteria criteria=marketCouponExample.createCriteria();
        Integer count =marketCouponMapper.updateByPrimaryKeySelective(marketCoupon);
        session.commit();
        session.close();
        return count;
    }

    @Override
    public Integer deleteCoupon(MarketCoupon marketCoupon) {
        SqlSession session= MybatisUtils.getSession();
        MarketCouponMapper marketCouponMapper=session.getMapper(MarketCouponMapper.class);
        MarketCouponExample marketCouponExample =new MarketCouponExample();
        MarketCouponExample.Criteria criteria=marketCouponExample.createCriteria();
        Integer i=marketCouponMapper.deleteByPrimaryKey(marketCoupon.getId());
        session.commit();
        session.close();
        return i;
    }


}
